package com.itheima.bos.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.constants.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

	@Action(value = "promotion_pageQuery", results = { @Result(name = "success", type = "json") })
	public String page_Query() {
		@SuppressWarnings("unchecked")
		PageBean<Promotion> pageBean = WebClient.create(Constants.BOS_MANAGEMENT_URL)
		.path("bos_management")
		.path("services")
		.path("promotionService")
		.path("pageQuery")
		.query("page", page)
		.query("rows", rows)
		.accept(MediaType.APPLICATION_JSON)
		.get(PageBean.class);
		ActionContext.getContext().getValueStack().push(pageBean);
		System.out.println(pageBean.getPageData().size());
		return SUCCESS;
	}
	
	@Action(value="promotion_showDetail")
	public String showDetail() throws IOException, Exception{
		//先判断id对应的Html是否存在
		String realPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
		File file = new File(realPath+"/"+model.getId()+".html");
		//如果html文件不存在,查询数据库,结合freemarker模板生成页面
		if(!file.exists()){
			//初始化对象
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/freemarker")));
			//获取模板对象
			Template template = configuration.getTemplate("promotion_detail.ftl");
			
			//动态数据
			Promotion promotion = WebClient.create(Constants.BOS_MANAGEMENT_URL)
			.path("bos_management")
			.path("services")
			.path("promotionService")
			.path("promotion")
			.path( model.getId())
			.accept(MediaType.APPLICATION_JSON)
			.type(MediaType.APPLICATION_JSON)
			.get(Promotion.class);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("promotion",promotion );
			//合并并输出
			template.process(map, new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
		}
		//设置响应头
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		//存在文件直接返回
		FileUtils.copyFile(file, ServletActionContext.getResponse().getOutputStream());
		
		return NONE;
	}
}
